package tp.pr3;



import java.util.Scanner;

import tp.pr3.instructions.*;
import tp.pr3.instructions.exceptions.*;
import tp.pr3.items.ItemContainer;
import tp.pr3.Interpreter;

public class RobotEngine {
	
	private boolean exit;
    private int fuel;
    private int recycledMaterial;
    private ItemContainer inventory;
    private NavigationModule navigationModule;
    
    public RobotEngine(City cityMap,
            Place initialPlace,
            Direction direction)
    {
    navigationModule= new NavigationModule(cityMap,initialPlace);
    navigationModule.initHeading(direction);
   	fuel=100;
   	recycledMaterial=0;
    inventory = new ItemContainer();
    exit = false;
    }
    
    public void addFuel(int fuel)
    {    
    this.fuel=this.fuel+fuel;	
    }
    
    public void addRecycledMaterial(int weight)
    {
    	recycledMaterial=recycledMaterial+weight;
    }
        
    public void communicateRobot(Instruction c)
    {
      try
      	{
    	  c.configureContext(this, navigationModule, inventory);
    	  c.execute();
      	}catch (InstructionExecutionException e1)
		{
      		System.out.print(e1.getMessage());
		}      	
    }
    
    public void printRobotState()
    {
    	String LINE_SEPARATOR = System.getProperty("line.separator");
		System.out.println( "      * My power is " + 			
   	       this.getFuel()	+	
              LINE_SEPARATOR +
              "      * My reclycled material is "+
              this.getRecycledMaterial());
    }
      
    public void requestQuit()
    {
    	if (!exit) exit = true;		
    }
       
    public void requestHelp()
    {
    	System.out.println(Interpreter.interpreterHelp());
    }
    
    
    public void startEngine()
    {
		
    	showText();
    	Scanner sc=new Scanner(System.in); 	
 		while(!exit)
		 {				
		  System.out.print("WALL·E> ");
		  String order =sc.nextLine();	 
		 try
			{
			    Instruction instruction=Interpreter.generateInstruction(order);
				instruction.configureContext(this, navigationModule, inventory);
				communicateRobot(instruction);				
			}	
		    catch (WrongInstructionFormatException e)
			{
				System.out.println(e.getMessage());
			} 
		 }
 		sc.close(); 
  } 
    
    public int getFuel()
    {
    	return fuel;
    }   
   
   
   public int getRecycledMaterial()
    {
    	return recycledMaterial;
    } 
   
   private void showText()
   {
        navigationModule.scanCurrentPlace();	
		System.out.println("");		
		System.out.println("WALL·E is looking at direction " + navigationModule.getCurrentHeading());
		printRobotState();
   } 
   
 
   
  
}     

